package com.diqizu.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;


import com.diqizu.dao.NewsDaoImpl;
import com.diqizu.entity.News;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;



/**
 * Servlet implementation class NewsServletAdmin
 */
@WebServlet("/admin/NewsServletAdmin")
public class NewsServletAdmin extends BaseServlet {
	NewsDaoImpl newsDao = new NewsDaoImpl();

  public void getNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 调用dao层的方法，获取所有员工
    ArrayList<News> News = newsDao.getAllList();
    // 调用结果工具类，将数据返回
    String res = Result.toClient(0, "新闻", News, News.size());
    PrintWriter out = resp.getWriter(); // 获取输出对象
    out.print(res); // 输出数据
    out.flush(); // 刷新
    out.close(); // 关闭
  }

  /**
   * 添加
   * 
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  public void insertNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 从页面上获取数据
    String title = req.getParameter("title");// 表单中name的值
    String context = req.getParameter("context");
    String typeId = req.getParameter("typeId");
    String time = req.getParameter("time");
    String isShow = req.getParameter("isShow");
    int typeIds = 0;
    if (typeId != null && !typeId.equals("")) {
      typeIds = Integer.valueOf(typeId);
    }

    // 调用dao层的方法，获取所有员工
    News newsdao = new News(0, title, context, typeIds, time,isShow);
    boolean res = newsDao.insertNews(newsdao);

    // 调用结果工具类，将数据返回页面
    PrintWriter out = resp.getWriter(); // 获取输出对象
    if (res) {
      out.write(Result.toClient(0, "添加成功"));
    } else {
      out.write(Result.toClient(1, "添加失败"));
    }

    out.flush(); // 刷新
    out.close(); // 关闭
  }

  /**
   * 修改
   * 
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */

  public void updateNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 从页面上获取数据
    String title = req.getParameter("title");// 表单中name的值
    String context = req.getParameter("context");
    String typeId = req.getParameter("typeId");
    String time = req.getParameter("time");
    String isShow = req.getParameter("isShow");
    String id = req.getParameter("id");
    int ids = 0;
    if (id != null && !id.equals("")) {
      ids = Integer.valueOf(id);
    }

    
    int typeIds = 0;
    if (typeId != null && !typeId.equals("")) {
      typeIds = Integer.valueOf(typeId);
    }

    // 调用dao层的方法，获取所有员工
    News newsdao = new News(ids, title, context, typeIds,time,isShow);

    boolean res = newsDao.updateNews(newsdao);

    // 调用结果工具类，将数据返回页面
    PrintWriter out = resp.getWriter(); // 获取输出对象
    if (res) {
      out.write(Result.toClient(0, "修改成功"));
    } else {
      out.write(Result.toClient(1, "修改失败"));
    }
    out.flush(); // 刷新
    out.close(); // 关闭
  }

  /**
   * 删除
   * 
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  public void delNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    // 从页面上获取数据
    String id = req.getParameter("id");

    int ids = 0;
    if (id != null && !id.equals("")) {
      ids = Integer.valueOf(id);
    }

    // 调用dao层的方法，获取所有员工
    boolean res = newsDao.delNews(ids);

    // 调用结果工具类，将数据返回页面
    PrintWriter out = resp.getWriter(); // 获取输出对象
    if (res) {
      out.write(Result.toClient(0, "删除成功"));
    } else {
      out.write(Result.toClient(1, "删除失败"));
    }
    out.flush(); // 刷新
    out.close(); // 关闭
  }
}
